import 'package:cesi/components/button.dart';
import 'package:flutter/material.dart';
import 'package:cesi/model/Coffee.dart';
import 'package:provider/provider.dart';

import '../model/shop.dart';
import '../theme/color.dart';

class CoffeeDetailsPage extends StatefulWidget {
  final Coffee coffee;
  const CoffeeDetailsPage({super.key, required this.coffee});

  @override
  State<CoffeeDetailsPage> createState() => _CoffeeDetailsPageState();
}

class _CoffeeDetailsPageState extends State<CoffeeDetailsPage> {
  //quantity
  int quantityCount = 0;

  //decrement quantity
  void decrementQuantity() {
    setState(() {
      if (quantityCount > 0) {
        quantityCount--;
      }
    });
  }

  //increment quantity
  void incrementQuantity() {
    setState(() {
      quantityCount++;
    });
  }

  //add to cart
  void addToCart() {
    if (quantityCount > 0) {
      //get access to shop
      final shop = Provider.of<Shop>(context, listen: false);

      //add to cart
      shop.addToCart(widget.coffee, quantityCount);

      //let user know add it was successful
      showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) => AlertDialog(
          backgroundColor: AppColors.primaryBrown,
          content: const Text(
            "add to cart successfully",
            style: TextStyle(color: Colors.white),
            textAlign: TextAlign.center,
          ),
          actions: [
            //ok button
            IconButton(
              onPressed: () {
                //pop once to remove dialog
                Navigator.pop(context);
                //pop twice to move to previous screen
                Navigator.pop(context);
              },
              icon: const Icon(Icons.done, color: Colors.white),
            ),
          ],
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.cream,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        foregroundColor: AppColors.textDark,
        elevation: 0,
      ),
      body: Column(
        children: [
          //listview of coffee
          Expanded(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 25.0),
              child: ListView(
                children: [
                  //image
                  Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20),
                      boxShadow: [
                        BoxShadow(
                          color: AppColors.lightBrown.withOpacity(0.3),
                          blurRadius: 15,
                          offset: const Offset(0, 5),
                        ),
                      ],
                    ),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(20),
                      child: Image.asset(widget.coffee.imagePath, height: 200),
                    ),
                  ),
                  const SizedBox(width: 5),

                  //rating
                  Row(
                    children: [
                      //Icon
                      Icon(Icons.star, color: AppColors.caramel),
                      const SizedBox(width: 5),
                      //rating
                      Text(
                        widget.coffee.rating,
                        style: TextStyle(
                          color: AppColors.textDark,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 10),

                  //coffee name
                  Text(
                    widget.coffee.name,
                    style: TextStyle(
                      fontSize: 28,
                      color: AppColors.textDark,
                      fontWeight: FontWeight.bold,
                    ),
                  ),

                  const SizedBox(height: 10),

                  //description
                  Text(
                    "Description",
                    style: TextStyle(
                      color: AppColors.textDark,
                      fontWeight: FontWeight.bold,
                      fontSize: 18,
                    ),
                  ),

                  const SizedBox(height: 10),

                  //main description
                  Text(
                    "这款风味豆子咖啡，精选优质咖啡豆，经过独特烘焙工艺，融合香草与焦糖的甜蜜风味。每一口都散发着浓郁的香气，口感醇厚顺滑，甜而不腻。无论是清晨唤醒味蕾，还是午后悠闲时光，它都是你的完美选择，带你开启一场美妙的味觉之旅。",
                    style: TextStyle(
                      color: AppColors.textLight,
                      fontSize: 14,
                      height: 2,
                    ),
                  ),
                ],
              ),
            ),
          ),

          //add to cart button
          Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(30),
                topRight: Radius.circular(30),
              ),
              boxShadow: [
                BoxShadow(
                  color: AppColors.lightBrown.withOpacity(0.3),
                  blurRadius: 20,
                  offset: const Offset(0, -5),
                ),
              ],
            ),
            padding: const EdgeInsets.all(20),
            child: Column(
              children: [
                //price+quantity
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    //price
                    Text(
                      "\$${widget.coffee.price}",
                      style: TextStyle(
                        color: AppColors.primaryBrown,
                        fontWeight: FontWeight.bold,
                        fontSize: 18,
                      ),
                    ),

                    //quantity
                    Row(
                      children: [
                        //minus button
                        Container(
                          decoration: BoxDecoration(
                            color: AppColors.lightBrown,
                            shape: BoxShape.circle,
                          ),
                          child: IconButton(
                            onPressed: decrementQuantity,
                            icon: Icon(Icons.remove, color: AppColors.textDark),
                          ),
                        ),

                        //quantity count
                        SizedBox(
                          width: 40,
                          child: Center(
                            child: Text(
                              quantityCount.toString(),
                              style: TextStyle(
                                color: AppColors.textDark,
                                fontWeight: FontWeight.bold,
                                fontSize: 18,
                              ),
                            ),
                          ),
                        ),

                        //increment button
                        Container(
                          decoration: BoxDecoration(
                            color: AppColors.primaryBrown,
                            shape: BoxShape.circle,
                          ),
                          child: IconButton(
                            onPressed: incrementQuantity,
                            icon: const Icon(Icons.add, color: Colors.white),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),

                const SizedBox(height: 25),

                //add to cart button
                MyButton(
                  text: "add to cart",
                  onTap: addToCart,
                  backgroundColor: AppColors.primaryBrown,
                  textColor: Colors.white,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
